﻿package com.projects.xenia;

import java.util.UUID;

import com.projects.xenia.global_modules.MainGlobalModule;

import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.CheckedTextView;
import android.widget.FilterQueryProvider;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

public class MainCommentsActivity extends Activity {

	private String DocId;
	private String Destination_Table;
	private String Source_Table;
	
	private Cursor Destination_Cursor;
	private Cursor Source_Cursor;
	
	private SimpleCursorAdapter Destination_Adapter;
	private SimpleCursorAdapter Source_Adapter;
	
	private ListView CommnetsListView; 
	private AutoCompleteTextView CommentsPromt;
	
	private Button CommentsAdd;
	private Button BackWard;
	
	private String SelectedCommentPromptId;
	private String SelectedCommentId;

	
	
	private void GetDestination_Cursor () {
		
		
		Destination_Cursor = MainGlobalModule.DBE.query(Destination_Table, new String []{"id as _id","hdoc_id","comment_id","descr"}, "hdoc_id=?", new String[]{DocId}, null, null,null );	
		 
				
	}
	
	private void GetSource_Cursor (String SubStringParameter) {		
		
		SubStringParameter = "%"+SubStringParameter+"%";
		Source_Cursor = MainGlobalModule.DBE.query(Source_Table, new String []{"id as _id ","descr"}, "descr like ? ", new String []{SubStringParameter}, null, null, "descr asc");		
				
	}
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {	
		
		super.onCreate(savedInstanceState);
		
		requestWindowFeature(Window.FEATURE_NO_TITLE);            
	    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);	    
	    
		setContentView(R.layout.comments_form);
		
		CommentsAdd = (Button)findViewById(R.id.CommentsAdd);
		CommnetsListView = (ListView)findViewById(R.id.CommentsList);		
		CommentsPromt = (AutoCompleteTextView)findViewById(R.id.CommentsPromt);
		
		Bundle DocExtras;
		DocExtras = getIntent().getExtras();
		DocId = DocExtras.getString("DocId");
		
		Destination_Table = DocExtras.getString("Destination_Table");
		GetDestination_Cursor ();
		
		Source_Table = DocExtras.getString("Source_Table");
		GetSource_Cursor ("");    
		
		Destination_Adapter = new SimpleCursorAdapter (this,android.R.layout.simple_list_item_checked,Destination_Cursor,new String[] {"descr"},new int[] {android.R.id.text1});
		CommnetsListView.setAdapter(Destination_Adapter);
		CommnetsListView.setOnItemClickListener(CommnetsListView_ItemClick);
		CommnetsListView.setFocusable(true);
		CommnetsListView.setItemsCanFocus(true);
		CommnetsListView.setFocusableInTouchMode(true);
		CommnetsListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
		
		CommnetsListView.setOnItemLongClickListener(new OnItemLongClickListener() {
			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1,int arg2, long arg3) {
		
				Destination_Cursor.moveToPosition(arg2);
				SelectedCommentId = Destination_Cursor.getString(0);				
				MainGlobalModule.DBE.delete(Destination_Table, "id=?",new String []{SelectedCommentId});
				Destination_Adapter.getCursor().requery();
				Destination_Adapter.notifyDataSetChanged();
				return true;
				
			}
			
		});
		
				
		Source_Adapter = new SimpleCursorAdapter (this,android.R.layout.simple_dropdown_item_1line,Source_Cursor,new String[] {"descr"},new int[] {android.R.id.text1});	
		
		Source_Adapter.setCursorToStringConverter(new SimpleCursorAdapter.CursorToStringConverter() 
        {
            public CharSequence convertToString(Cursor cursor) 
            {
            	SelectedCommentPromptId = cursor.getString(0); 
                return cursor.getString(1);
            }
            
        });
		
		BackWard = (Button)findViewById(R.id.BackwardButt);
		BackWard.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				finish ();
			}
		});
		
				
		CommentsPromt.setAdapter(Source_Adapter);
		CommentsPromt.setThreshold(0);
		CommentsPromt.setOnClickListener (CommentsPromt_Click);				
		
		Source_Adapter.setFilterQueryProvider(new FilterQueryProvider() {			
			@Override
			public Cursor runQuery(CharSequence constraint) {
				if (constraint==null){
					constraint = "";
				} 
				GetSource_Cursor(constraint.toString());
				return Source_Cursor;
			}
		});
		
		
		CommentsAdd.setOnClickListener(CommentsAdd_Click);
				
	}
	
	public OnItemClickListener CommnetsListView_ItemClick = new OnItemClickListener() {
		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {		
			Destination_Cursor.moveToPosition(arg2);
			SelectedCommentId = Destination_Cursor.getString(0);
	        
		}
	};
	
	public OnClickListener CommentsAdd_Click = new OnClickListener() {		
		@Override
		public void onClick(View v) {			
			String CommentsPromptText = CommentsPromt.getText().toString();
			if ((CommentsPromptText.trim()).equals("")==false) {
				ContentValues InsertValues = new ContentValues ();			
				InsertValues.put("id", UUID.randomUUID().toString().toUpperCase());
				InsertValues.put("hdoc_id", DocId);
				InsertValues.put("comment_id",SelectedCommentPromptId);
				InsertValues.put("descr", CommentsPromptText);
				MainGlobalModule.DBE.insert(Destination_Table, null,InsertValues);
				SelectedCommentPromptId = null;
				CommentsPromt.setText("");
				Destination_Adapter.getCursor().requery();
				Destination_Adapter.notifyDataSetChanged();
			}
						
		}
	};

	public OnClickListener CommentsPromt_Click  = new OnClickListener() {		
		@Override
		public void onClick(View v) {
			CommentsPromt.showDropDown();	
		}
			
	};
	
    public boolean onCreateOptionsMenu (Menu menu){
    	menu.add(Menu.NONE,1,Menu.NONE,"Удалить");    	
    	menu.add(Menu.NONE,2,Menu.NONE,"Удалить все");
    	menu.add(Menu.NONE,3,Menu.NONE,"ОК");
    	return true;    	
    }
    
    public boolean onOptionsItemSelected (MenuItem item){
    	
    	switch (item.getItemId()) {

    		case 1:{ 
    			if (SelectedCommentId!=null) { 
    				MainGlobalModule.DBE.delete(Destination_Table, "id=?",new String []{SelectedCommentId});
    				Destination_Adapter.getCursor().requery();
    				Destination_Adapter.notifyDataSetChanged();
    				CommnetsListView.clearChoices();
    			}
    		}
    		break;
    		
    		case 2:{

    			MainGlobalModule.DBE.delete(Destination_Table, "hdoc_id=?",new String []{DocId});
    			Destination_Adapter.getCursor().requery();
    			Destination_Adapter.notifyDataSetChanged();			
    		}
    		break;
    		
    		case 3:{
    			finish ();
    		}
    		break;
    		
    	}
    	
    	return true;    	
    	    
    }

	
}
